{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-27T14:09:58.429868200Z",
     "start_time": "2023-11-27T14:09:58.423868400Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取整个文件read()\n",
      "\n",
      "文件打开后，可以使用read()读取所打开的文件，使用read()读取时，所有的文件内容将以一个字符串方式被读取然后存入字符串变量内，未来只要打印此字符串变量相当于可以打印整个文件内容​。\n"
     ]
    }
   ],
   "source": [
    "fn = \"a.txt\"  # 定义一个字符串变量fn，值为\"a.txt\"\n",
    "file_Obj = open(fn, encoding=\"utf-8\")  # 打开文件\"a.txt\"，使用utf-8编码方式，并将文件对象赋值给变量file_Obj\n",
    "data = file_Obj.read()  # 读取文件的内容并将其赋值给变量data\n",
    "print(data)  # 打印变量data的内容\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取整个文件read()\n",
      "\n",
      "文件打开后，可以使用read()读取所打开的文件，使用read()读取时，所有的文件内容将以一个字符串方式被读取然后存入字符串变量内，未来只要打印此字符串变量相当于可以打印整个文件内容​。\n"
     ]
    }
   ],
   "source": [
    "with open(\"a.txt\",encoding=\"utf-8\") as  f:\n",
    "    data = f.read()\n",
    "    print(data)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-27T14:17:19.868996Z",
     "start_time": "2023-11-27T14:17:19.836270300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['读取整个文件read()\\n', '\\n', '文件打开后，可以使用read()读取所打开的文件，使用read()读取时，所有的文件内容将以一个字符串方式被读取然后存入字符串变量内，未来只要打印此字符串变量相当于可以打印整个文件内容\\u200b。']\n",
      "读取整个文件read()\n",
      "\n",
      "文件打开后，可以使用read()读取所打开的文件，使用read()读取时，所有的文件内容将以一个字符串方式被读取然后存入字符串变量内，未来只要打印此字符串变量相当于可以打印整个文件内容​。\n"
     ]
    }
   ],
   "source": [
    "# 打开文件\n",
    "with open('a.txt', 'r', encoding=\"utf-8\") as file:\n",
    "    # 使用 readlines() 读取所有行并存储在列表中\n",
    "    lines = file.readlines()\n",
    "    print(lines)\n",
    "\n",
    "# 遍历列表中的每一行\n",
    "for line in lines:\n",
    "    # 去除每行末尾的换行符并打印输出\n",
    "    print(line.strip())\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-27T14:29:23.067398600Z",
     "start_time": "2023-11-27T14:29:23.061397200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取整个文件read()\n",
      "\n",
      "文件打开后，可以使用read()读取所打开的文件，使用read()读取时，所有的文件内容将以一个字符串方式被读取然后存入字符串变量内，未来只要打印此字符串变量相当于可以打印整个文件内容​。\n"
     ]
    }
   ],
   "source": [
    "with open('a.txt', 'r', encoding=\"utf-8\") as file:  # 打开文件a.txt，以只读模式并指定编码为utf-8\n",
    "    for line in file:  # 遍历文件的每一行\n",
    "        print(line.strip())  # 打印去首尾空格的每一行"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-27T14:31:57.227542500Z",
     "start_time": "2023-11-27T14:31:57.213539600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取整个文件read()\n",
      "\n",
      "\n",
      "\n",
      "读取文件的时候，可以使用read()读取所打开的文件，使用read()读取时，所有的文件内容将以一个字符串方式被读取然后存入字符串变量内，\n",
      "\n",
      "未来只要打印此字符串变量相当于可以打印整个文件内容。\n"
     ]
    }
   ],
   "source": [
    "# 字符串的替换\n",
    "with open('a.txt', 'r', encoding=\"utf-8\") as file:\n",
    "    for line in file:\n",
    "        print(line.replace('文件打开后', '读取文件的时候'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-29T11:36:57.105377700Z",
     "start_time": "2023-11-29T11:36:57.091865800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找到了，索引是77\n"
     ]
    }
   ],
   "source": [
    "# 数据搜寻使用find()\n",
    "with open(\"a.txt\",\"r\",encoding=\"utf-8\") as file:\n",
    "    f = file.read()\n",
    "    num = f.find(\"变量\")\n",
    "    if num!= -1:\n",
    "        print(f\"找到了，索引是{num}\")\n",
    "    else:\n",
    "        print(\"没有找到\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-29T11:48:09.137167400Z",
     "start_time": "2023-11-29T11:48:09.125650100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 写入文件\n",
    "with open(\"b.txt\",\"w\",encoding=\"utf-8\") as file:\n",
    "    file.write(\"我是写入的内容\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-29T12:25:46.626769400Z",
     "start_time": "2023-11-29T12:25:46.609825600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# 追加文件\n",
    "with open(\"b.txt\",\"a\",encoding=\"utf-8\") as file:\n",
    "    file.write(\"我是追加的内容\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-29T12:40:15.720474700Z",
     "start_time": "2023-11-29T12:40:15.704079300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
